<template>
  <div>
    <el-button type="info" icon="el-icon-caret-left" @click="$router.go(-1)" size="mini">返回</el-button>
    <el-form ref="Attendance" :model="AttendanceInfo" size="small" :rules="Edit">
      <el-row>
        <el-col :span="10">
          <el-form-item label="类型" prop="TypeID"><br>
            <el-select v-model="AttendanceInfo.TypeID" placeholder="选择类型" style="width:100%">
              <el-option v-for="(item,index) in options" :key="index" :label="item.TypeName_CHS" :value="item.ID">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4"><br></el-col>
        <el-col :span="10">
          <el-form-item label="备注" prop="Description">
            <el-input v-model="AttendanceInfo.Description" placeholder="请输入你的备注"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <div style="padding-top:10px;border-top:1px solid #edeff2;">
        <el-button type="primary" @click="submit('Attendance')" icon="el-icon-check" size="small">保存</el-button>
        <el-button type="success" @click="$router.go(-1)" icon="el-icon-close" size="small">取消</el-button>
      </div>

    </el-form>
  </div>
</template>

<script>
export default {
  data () {
    return {
      options: [
      ],
      AttendanceInfo: {
        TypeID: null
      },
      Type: 'Add',
      Edit: {
        TypeID: [
          { required: true, message: '请输入你的类型', trigger: 'blur' }
        ]
      }
    }
  },
  mounted () {
    this.$axios({
      url: '/HrApi/Attendance/GetTypeList',
      method: 'GET',
    }).then(res => {
      if (!res.data.code) {
        this.options = res.data.data;
      }
    })
    //编辑
    if (Object.keys(this.$route.query).length) {
      this.Type = "Edit";
      this.$axios({
        url: '/HrApi/Attendance/GetAttendanceInfo',
        method: 'GET',
        params: {
          pk_Attendance: this.$route.query["pk_Attendance"]
        }
      }).then(res => {
        if (!res.data.code) {
          this.AttendanceInfo = res.data.data
        } else {
          this.$message.error(res.data.msg)
        }
      })
    }
  },
  methods: {
    submit (Attendance) {
      //表单验证
      this.$refs[Attendance].validate((valid) => {
        if (!valid) {
          return false;
        } else {
          //执行请求
          this.$axios({
            url: "/HrApi/Attendance/UpdateAttendanceList",
            method: "POST",
            data: this.$Qs.stringify({
              AttendanceInfo: JSON.stringify(this.AttendanceInfo),
              type: this.Type,
              pk_User: this.$store.state.UserInfo.pk_User
            })
          }).then(res => {
            if (!res.data.code) {
              this.ListDt = res.data.data;
              this.$message.success(res.data.msg)
              if (this.Type == "Add") {
                this.$refs['Attendance'].resetFields();
              }

            } else {
              this.$message.error(res.data.msg);
            }
          })
        }
      })
    }
  }
}
</script>